package cz.semjobKB.virtuoso.service.impl;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import virtuoso.jena.driver.VirtGraph;
import cz.semjobKB.knowledge.api.IExtractedKnowledge;
import cz.semjobKB.virtuoso.service.api.IDatabaseService;
import cz.semjobKB.virtuoso.dao.GraphDetails;
import cz.semjobKB.virtuoso.dao.Search;
import cz.semjobKB.virtuoso.dao.VirtuosoDao;
import cz.semjobKB.virtuoso.document.api.IDocumentDetail;
import cz.semjobKB.virtuoso.document.api.IResourceDetail;
import cz.semjobKB.virtuoso.document.api.ITerminologyDetail;
import cz.semjobKB.virtuoso.document.api.ITripleDbItem;
import cz.semjobKB.virtuoso.utils.ELanguage;
import cz.semjobKB.virtuoso.utils.VirtuosoUtils;

@Service
public class DatabaseService implements IDatabaseService {
	
	@Autowired
	private VirtuosoUtils virtuosoUtils;
	
	@Autowired
	private VirtuosoDao virtuosoDao;
	
	@Autowired
	private GraphDetails graphDetails;
	
	@Autowired
	private Search search;
	
	private static Logger logger = Logger.getLogger(DatabaseService.class);
	
	@Value( "${db.url}" )
	private String dbUrl;
	
	@Value( "${db.user}" )
	private String dbUser;
	
	@Value( "${db.pass}" )
	private String dbPass;
	
	public DatabaseService() {	
	}
	
	
	public synchronized IDocumentDetail storeDocument(IExtractedKnowledge extractedKnowledge,
			File documentFile, ELanguage language) {
		logger.info("Document will be stored in the database");
		String documentName = documentFile.getName();
		documentName = documentName.replaceAll(" ", "_");
		documentName = FilenameUtils.removeExtension(documentName);
		
		VirtGraph documentGraph = new VirtGraph(virtuosoUtils.getKnownGraphsUris().get("document") + documentName,
				dbUrl, dbUser, dbPass);
				
		Integer termCount =  virtuosoDao.storeTerms(extractedKnowledge.getTriples(), language);
		Integer predicateCount = virtuosoDao.storePredicates(extractedKnowledge.getTriples(), language);
		virtuosoDao.storeTerminology(documentGraph, extractedKnowledge.getTerminology(), language);
		
		virtuosoDao.persistDocument(documentGraph, extractedKnowledge, termCount, predicateCount, documentFile);
		String docName = documentFile.getName().replaceAll("." + FilenameUtils.getExtension(documentFile.getName()), "");
		return getDocumentDetails(docName);
		
	}

	
	public IDocumentDetail getDocumentDetails(String documentName) {		
		return graphDetails.getDocumentDetail(documentName);
	}

	
	public List<IResourceDetail> getResourceDetail(String resourceName) {		
		return graphDetails.getResourceDetail(resourceName, false);
	}

	
	public List<ITerminologyDetail> getTerminologyDetail(String terminologyName) {
		return graphDetails.getTerminologyDetail(terminologyName);
	}

	
	public List<ITripleDbItem> searchTriples(String subject, String predicate,
			String object) {		
		return search.searchTriples(subject, predicate, object);
	}

	
	public List<IDocumentDetail> searchDocument(String subject, String predicate,
			String object) {
		return search.searchDocumentByTriples(subject, predicate, object);
	}

	
	public List<IDocumentDetail> searchDocument(String terminology,
			double frequencyLimit) {
		return search.searchByTerminology(terminology, frequencyLimit);
	}	
}
